package com.sun.boostrap.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.annotation.Log;
import com.sun.boostrap.service.EnhanceUserInfoService;
import com.sun.common.Result;
import com.sun.common.ResultCodeEnum;
import com.sun.entity.EnhanceUserInfo;
import com.sun.event.EventListenParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "用户接口测试")
@RestController
@RequestMapping("/user")
public class EnhanceUserController {

    @Resource
    private EnhanceUserInfoService userInfoService;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @ApiOperation("查询用户列表信息")
    @Log(value = "userLog")
    @GetMapping("/list")
    public Result<List<EnhanceUserInfo>> selectAllUserList() {
        return Result.build(userInfoService.selectUserInfoList(), ResultCodeEnum.SUCCESS);
    }


    /**
     * 获取发布的事件的信息
     *
     * @return
     */
    @EventListener(value = {EventListenParam.class})
    public void test(EventListenParam param) throws JsonProcessingException {
        Map<String, String> map = param.getParamMap();
        String value = objectMapper.writeValueAsString(map);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.info("获取到的事件:[{}]",value);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

}
